package com.fqgj.xjd.user.service.impl;
/**
 * Created by zhangxingyun on 17/7/23.
 */

import com.alibaba.fastjson.JSON;
import com.fqgj.common.api.Page;
import com.fqgj.common.api.Response;
import com.fqgj.exception.common.ApiErrorException;
import com.fqgj.exception.enums.BasicErrorCodeEnum;
import com.fqgj.xjd.user.client.UserBehaviorService;
import com.fqgj.xjd.user.client.enums.FeedbackTypeEnum;
import com.fqgj.xjd.user.client.request.*;
import com.fqgj.xjd.user.client.response.*;
import com.fqgj.xjd.user.dao.*;
import com.fqgj.xjd.user.entity.*;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName:
 * @Description: 用户行为信息维护服务
 * @Author zhangxingyun
 * @Create 2017-07-23 17:31
 */
@Service("userBehaviorService")
public class UserBehaviorServiceImpl implements UserBehaviorService {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserBehaviorServiceImpl.class);

    @Autowired
    private UserBehaviorRegisterDao userBehaviorRegisterDao;

    @Autowired
    private UserBehaviorGpsDao userBehaviorGpsDao;

    @Autowired
    private UserBehaviorWifiDao userBehaviorWifiDao;

    @Autowired
    private UserBehaviorFeedbackDao userBehaviorFeedbackDao;

    @Autowired
    private UserAccessDao userAccessDao;

    @Autowired
    private UserBehaviorBodyguardDao userBehaviorBodyguardDao;

    @Autowired
    private UserBehaviorLoginDao userBehaviorLoginDao;

    @Autowired
    private UserAssistanceFeedbackDao userAssistanceFeedbackDao;

    @Autowired
    private UserDailyActivityInfoDao userDailyActivityInfoDao;

    @Autowired
    private UserBehaviorEventDao userBehaviorEventDao;

    @Autowired
    private UserBehaviorBuriedDao userBehaviorBuriedDao;

    @Autowired
    private ThirdCallHistoryDao thirdCallHistoryDao;

    @Autowired
    private OpenApiBlackListMatchingResultDao openApiBlackListMatchingResultDao;

    /**
     * 获取用户最新GPS信息
     *
     * @param userCode
     * @return
     */
    @Override
    public Response<UserGps> getLatestUserGps(String userCode) {
        UserBehaviorGpsEntity userBehaviorGpsEntity = userBehaviorGpsDao.selectLatestUserGpsByUserCode(userCode);

        if (null != userBehaviorGpsEntity) {
            UserGps userGps = new UserGps();
            BeanUtils.copyProperties(userBehaviorGpsEntity, userGps);
            return Response.ok().putData(userGps);
        }
        return Response.ok();
    }

    /**
     * 保存GPS信息
     *
     * @param userCode
     * @param usergps
     * @return
     */
    @Override
    public Response<Boolean> addUserGps(String userCode, UserGps usergps) {
        UserBehaviorGpsEntity userBehaviorGpsEntity = new UserBehaviorGpsEntity();

        BeanUtils.copyProperties(usergps, userBehaviorGpsEntity);
        userBehaviorGpsEntity.setUserCode(userCode);
        //删除老数据
        userBehaviorGpsDao.updateByUserCode(userCode);
        //新增新数据
        userBehaviorGpsDao.insert(userBehaviorGpsEntity);

        return Response.ok().putData(true);
    }

    /**
     * 获取用户最新Wi-Fi信息
     *
     * @param userCode
     * @return
     */
    @Override
    public Response<UserWifi> getLatestUserWifi(String userCode) {

        UserBehaviorWifiEntity userBehaviorWifiEntity = userBehaviorWifiDao.selectLatestUserWifiByUserCode(userCode);
        if (null != userBehaviorWifiEntity) {
            UserWifi userWifi = new UserWifi();
            BeanUtils.copyProperties(userBehaviorWifiEntity, userWifi);
            return Response.ok().putData(userWifi);
        }

        return Response.ok();
    }

    /**
     * 保存客户WI_FI信息
     *
     * @param userCode
     * @param userWifi
     * @return
     */
    @Override
    public Response<Boolean> addUserWifi(String userCode, UserWifi userWifi) {

        UserBehaviorWifiEntity userBehaviorWifiEntity = new UserBehaviorWifiEntity();
        BeanUtils.copyProperties(userWifi, userBehaviorWifiEntity);
        userBehaviorWifiEntity.setUserCode(userCode);
        userBehaviorWifiDao.insert(userBehaviorWifiEntity);
        return Response.ok().putData(true);
    }

    /**
     * 批量新增
     */
    @Override
    public Response<Boolean> batchAddWifi(List<UserWifi> userWifi) {
        if (CollectionUtils.isEmpty(userWifi)) {
            return Response.ok().putData(null);
        }

        List<UserBehaviorWifiEntity> userBehaviorWifiEntities = userWifi.stream().map(userWifi1 -> {
            UserBehaviorWifiEntity entity = new UserBehaviorWifiEntity();
            BeanUtils.copyProperties(userWifi1, entity);
            return entity;
        }).collect(Collectors.toList());

        int num = userBehaviorWifiDao.batchAddWifi(userBehaviorWifiEntities);
        return Response.ok().putData(num > 0);

    }

    /**
     * 保存客户注册信息
     *
     * @param userCode
     * @param userAppInfo
     * @return
     */
    @Override
    public Response<Boolean> addUserRegister(String userCode, UserAppInfo userAppInfo) {
        if (userAppInfo != null) {
            UserAccessEntity userAccessEntity = userAccessDao.selectUserAccessByUserCode(userCode);
            UserBehaviorRegisterEntity userBehaviorRegisterEntity = new UserBehaviorRegisterEntity();
            userBehaviorRegisterEntity.setUserCode(userCode);
            userBehaviorRegisterEntity.setChannel(userAppInfo.getChannel());
            userBehaviorRegisterEntity.setChannelId(userAppInfo.getChannelId());
            userBehaviorRegisterEntity.setClientId(userAppInfo.getClientId());
            userBehaviorRegisterEntity.setPlatform(userAppInfo.getPlatform().getType());
            userBehaviorRegisterEntity.setVersion(userAppInfo.getVersion());
            userBehaviorRegisterEntity.setAppCode(userAccessEntity.getAppCode());
            userBehaviorRegisterEntity.setIp(userAppInfo.getIp());
            userBehaviorRegisterEntity.setVersionCode(userAppInfo.getVersionCode());
            userBehaviorRegisterEntity.setCreateDate(new Date());
            userBehaviorRegisterEntity.setPackageName(userAppInfo.getPackageName());
            userBehaviorRegisterEntity.setAppsflyerId(userAppInfo.getAppsflyerId());
            userBehaviorRegisterDao.insert(userBehaviorRegisterEntity);
        }
        return Response.ok().putData(true);
    }

    /**
     * 更新客户注册信息
     *
     * @param userCode
     * @param userAppInfo
     * @return
     */
    @Override
    public Response<Boolean> updateUserRegister(String userCode, UserAppInfo userAppInfo) {
        UserBehaviorRegisterEntity userBehaviorRegisterEntity = userBehaviorRegisterDao.selectUserBehaviorRegisterByUserCodeAndAppCode(userCode);

        if (null == userBehaviorRegisterEntity) {
            this.addUserRegister(userCode, userAppInfo);
        } else {
            if (StringUtils.isNotBlank(userAppInfo.getChannel())) {
                userBehaviorRegisterEntity.setChannel(userAppInfo.getChannel());
            }
            if (StringUtils.isNotBlank(userAppInfo.getClientId())) {
                userBehaviorRegisterEntity.setClientId(userAppInfo.getClientId());
            }
            if (StringUtils.isNotBlank(userAppInfo.getVersion())) {
                userBehaviorRegisterEntity.setVersion(userAppInfo.getVersion());
            }
            if (StringUtils.isNotBlank(userAppInfo.getVersionCode())) {
                userBehaviorRegisterEntity.setVersionCode(userAppInfo.getVersionCode());
            }
            if (StringUtils.isNotEmpty(userAppInfo.getPackageName())) {
                userBehaviorRegisterEntity.setPackageName(userAppInfo.getPackageName());
            }
            userBehaviorRegisterDao.updateByPrimaryKey(userBehaviorRegisterEntity);
        }
        return Response.ok().putData(true);
    }

    /**
     * 更新客户客户号信息
     *
     * @param userCode
     * @param clientId
     * @return
     */
    @Override
    public Response<Boolean> updateOrAddUserClientIdByUserCode(String userCode, String clientId) {
        UserBehaviorRegisterEntity userBehaviorRegisterEntity = userBehaviorRegisterDao.selectUserBehaviorRegisterByUserCodeAndAppCode(userCode);

        if (null != userBehaviorRegisterEntity) {
            userBehaviorRegisterEntity.setClientId(clientId);
            userBehaviorRegisterDao.updateByPrimaryKey(userBehaviorRegisterEntity);
        }
        return Response.ok().putData(true);
    }

    /**
     * 根据客户号获取注册信息
     *
     * @param clientId
     * @return
     */
    @Override
    public Response<List<UserBehaviorRegister>> getUserRegisterByClientId(String clientId) {

        List<UserBehaviorRegisterEntity> userBehaviorRegisterEntityList = userBehaviorRegisterDao.selectUserRegisterByClientId(clientId);

        if (userBehaviorRegisterEntityList.size() > 0) {
            List<UserBehaviorRegister> userBehaviorRegisterList = new ArrayList<UserBehaviorRegister>();
            for (UserBehaviorRegisterEntity userBehaviorRegisterEntity : userBehaviorRegisterEntityList) {
                UserBehaviorRegister userBehaviorRegister = new UserBehaviorRegister();
                BeanUtils.copyProperties(userBehaviorRegisterEntity, userBehaviorRegister);
                userBehaviorRegisterList.add(userBehaviorRegister);
            }
            return Response.ok().putData(userBehaviorRegisterList);
        }

        return Response.ok();
    }

    /**
     * 分页查询客户ID列表
     *
     * @param page
     * @return
     */
    @Override
    public Response<List<UserBehaviorRegister>> getUserRegisterListByPage(Page page) {
        List<UserBehaviorRegisterEntity> userBehaviorRegisterEntityList = userBehaviorRegisterDao.selectUserRegisterByPage(page);

        if (userBehaviorRegisterEntityList.size() > 0) {
            List<UserBehaviorRegister> userBehaviorRegisterList = new ArrayList<UserBehaviorRegister>();
            for (UserBehaviorRegisterEntity userBehaviorRegisterEntity : userBehaviorRegisterEntityList) {
                UserBehaviorRegister userBehaviorRegister = new UserBehaviorRegister();
                BeanUtils.copyProperties(userBehaviorRegisterEntity, userBehaviorRegister);
                userBehaviorRegisterList.add(userBehaviorRegister);
            }
            return Response.ok().putData(userBehaviorRegisterList);
        }

        return Response.ok();
    }

    /**
     * 保存用户反馈
     *
     * @param userCode
     * @param title
     * @param content
     * @return
     */
    @Override
    public Response<Boolean> saveFeedback(String userCode, String title, String content) {
        UserAccessEntity userAccessEntity = userAccessDao.selectUserAccessByUserCode(userCode);
        UserBehaviorFeedbackEntity userBehaviorFeedbackEntity = new UserBehaviorFeedbackEntity();
        userBehaviorFeedbackEntity.setUserCode(userCode);
        userBehaviorFeedbackEntity.setTitle(title);
        userBehaviorFeedbackEntity.setContent(content);
        userBehaviorFeedbackEntity.setAppCode(userAccessEntity != null ? userAccessEntity.getAppCode() : "");
        userBehaviorFeedbackDao.insert(userBehaviorFeedbackEntity);
        return Response.ok().putData(true);
    }

    @Override
    public Response<Boolean> saveFeedback(UserFeedBackRequest userFeedBackRequest) {
        UserAccessEntity userAccessEntity = userAccessDao.selectUserAccessByUserCode(userFeedBackRequest.getUserCode());
        String mobile = userAccessEntity != null ? userAccessEntity.getMobile() : "";
        UserBehaviorFeedbackEntity userBehaviorFeedbackEntity = new UserBehaviorFeedbackEntity();
        userBehaviorFeedbackEntity.setUserCode(userFeedBackRequest.getUserCode());
        userBehaviorFeedbackEntity.setTitle(userFeedBackRequest.getTitle());
        userBehaviorFeedbackEntity.setContent(userFeedBackRequest.getContent());
        userBehaviorFeedbackEntity.setAppCode(userAccessEntity != null ? userAccessEntity.getAppCode() : "");
        userBehaviorFeedbackEntity.setEmail(userFeedBackRequest.getEmail());
        userBehaviorFeedbackEntity.setName(userFeedBackRequest.getName());
        userBehaviorFeedbackEntity.setImageUrl(userFeedBackRequest.getImageUrl());
        userBehaviorFeedbackEntity.setStatus(0);
        userBehaviorFeedbackEntity.setAnswerType(FeedbackTypeEnum.USER.getCode());
        userBehaviorFeedbackEntity.setMobile(StringUtils.isNotBlank(userFeedBackRequest.getMobile()) ? userFeedBackRequest.getMobile() : mobile);
        userBehaviorFeedbackDao.insert(userBehaviorFeedbackEntity);
        return Response.ok().putData(true);
    }

    @Override
    public Response<Boolean> updateFeedback(UserFeedBackAnswerRequest userFeedBackRequest) {
        if (userFeedBackRequest.getId() == null) {
            return Response.error(BasicErrorCodeEnum.PARAM_VALID_ERROR, "ID can not be empty");
        }
        UserBehaviorFeedbackEntity userBehaviorFeedbackEntity = userBehaviorFeedbackDao.selectByPrimaryKey(userFeedBackRequest.getId());
        if (userBehaviorFeedbackEntity == null) {
            return Response.error(BasicErrorCodeEnum.PARAM_VALID_ERROR, "feedback not exist");
        }
        UserBehaviorFeedbackEntity entity = new UserBehaviorFeedbackEntity();
        entity.setId(userFeedBackRequest.getId());
        entity.setStatus(userFeedBackRequest.getStatus());
        entity.setHandlerAdminId(userFeedBackRequest.getHandlerAdminId());
        entity.setOwnerId(userFeedBackRequest.getOwnerId());
        entity.setHandlerLog(userFeedBackRequest.getHandlerLog());
        entity.setNoticeStatus(userFeedBackRequest.getNoticeStatus());
        userBehaviorFeedbackDao.updateByPrimaryKey(entity);
        return Response.ok().putData(true);
    }

    @Override
    public Response<Boolean> answerFeedback(UserFeedBackAnswerRequest userFeedBackRequest) {
        if (StringUtils.isEmpty(userFeedBackRequest.getUserCode()) || StringUtils.isEmpty(userFeedBackRequest.getContent())) {
            return Response.error(BasicErrorCodeEnum.PARAM_VALID_ERROR, "userCode or content can not be empty");
        }
        UserBehaviorFeedbackEntity userBehaviorFeedbackEntity = new UserBehaviorFeedbackEntity();
        userBehaviorFeedbackEntity.setStatus(userFeedBackRequest.getStatus());
        userBehaviorFeedbackEntity.setHandlerAdminId(userFeedBackRequest.getHandlerAdminId());
        userBehaviorFeedbackEntity.setOwnerId(userFeedBackRequest.getOwnerId());
        userBehaviorFeedbackEntity.setHandlerLog(userFeedBackRequest.getHandlerLog());
        userBehaviorFeedbackEntity.setNoticeStatus(userFeedBackRequest.getNoticeStatus());
        userBehaviorFeedbackEntity.setContent(userFeedBackRequest.getContent());
        userBehaviorFeedbackEntity.setAnswerType(FeedbackTypeEnum.CUSTOMER_SERVICE.getCode());
        userBehaviorFeedbackEntity.setUserCode(userFeedBackRequest.getUserCode());
        userBehaviorFeedbackEntity.setNoticeStatus(1);
        userBehaviorFeedbackDao.insert(userBehaviorFeedbackEntity);

        Map<String, Object> param = new HashMap<>();
        param.put("userCode", userFeedBackRequest.getUserCode());
        param.put("answerType", FeedbackTypeEnum.USER.getCode());
        param.put("status", 0);
        List<UserBehaviorFeedbackEntity> entities = userBehaviorFeedbackDao.selectByParams(param);
        if (CollectionUtils.isNotEmpty(entities)) {
            List<Long> ids = Lists.newArrayList();
            for (UserBehaviorFeedbackEntity entity : entities) {
                ids.add(entity.getId());
            }
            userBehaviorFeedbackDao.updateByIds(ids);
        }
        return Response.ok().putData(true);
    }

    @Override
    public Response<UserFeedbackList> getUserFeedbackListByPage(UserFeedbackListRequest userFeedbackListRequest) {
        List<UserBehaviorFeedbackEntity> userBehaviorFeedbackEntities = userBehaviorFeedbackDao.selectUserFeedbackByPage(userFeedbackListRequest.getPage(), userFeedbackListRequest.getStatus(), userFeedbackListRequest.getOwnerId(), userFeedbackListRequest.getNoticeStatus(), userFeedbackListRequest.getAppCode());
        if (CollectionUtils.isEmpty(userBehaviorFeedbackEntities)) {
            return Response.ok().putData(new UserFeedbackList());
        }
        UserFeedbackList userFeedbackList = new UserFeedbackList();
        List<UserFeedBackRO> feedBacks = Lists.newArrayList();
        for (UserBehaviorFeedbackEntity entity : userBehaviorFeedbackEntities) {
            UserFeedBackRO userFeedBackRO = new UserFeedBackRO();
            BeanUtils.copyProperties(entity, userFeedBackRO);
            userFeedBackRO.setId(entity.getId());
            userFeedBackRO.setSubmitDate(entity.getGmtCreate());
            feedBacks.add(userFeedBackRO);
        }
        userFeedbackList.setUserFeedBacks(feedBacks);
        userFeedbackList.setTotalCount(userFeedbackListRequest.getPage().getTotalCount());
        userFeedbackList.setTotalPage(userFeedbackListRequest.getPage().getTotalPage());
        return Response.ok().putData(userFeedbackList);
    }


    @Override
    public Response<UserFeedbackList> getUserFeedbackListByUserCode(String userCode) {
        List<UserBehaviorFeedbackEntity> userBehaviorFeedbackEntities = userBehaviorFeedbackDao.selectUserFeedbackByUserCode(userCode);
        if (CollectionUtils.isEmpty(userBehaviorFeedbackEntities)) {
            return Response.ok().putData(null);
        }

        UserFeedbackList userFeedbackList = new UserFeedbackList();
        List<UserFeedBackRO> feedBacks = Lists.newArrayList();
        for (UserBehaviorFeedbackEntity entity : userBehaviorFeedbackEntities) {
            UserFeedBackRO userFeedBackRO = new UserFeedBackRO();
            BeanUtils.copyProperties(entity, userFeedBackRO);
            userFeedBackRO.setId(entity.getId());
            userFeedBackRO.setSubmitDate(entity.getGmtCreate());
            feedBacks.add(userFeedBackRO);
        }
        userFeedbackList.setUserFeedBacks(feedBacks);
        return Response.ok().putData(userFeedbackList);
    }

    @Override
    public Response<UserFeedBackRO> getUserFeedbackListId(Long feedbackId) {
        UserBehaviorFeedbackEntity entity = userBehaviorFeedbackDao.selectByPrimaryKey(feedbackId);
        if (entity == null) {
            return Response.ok().putData(null);
        }

        UserFeedBackRO userFeedBackRO = new UserFeedBackRO();
        BeanUtils.copyProperties(entity, userFeedBackRO);
        userFeedBackRO.setId(entity.getId());
        userFeedBackRO.setSubmitDate(entity.getGmtCreate());

        return Response.ok().putData(userFeedBackRO);
    }

    @Override
    public Response<Boolean> saveDifficultiesFeedback(UserDifficultiesFeedBackRequest userDifficultiesFeedBackRequest) {
        if (StringUtils.isBlank(userDifficultiesFeedBackRequest.getUserCode())) {
            return Response.error(BasicErrorCodeEnum.PARAM_VALID_ERROR, "userCode can not be empty");
        }
        if (StringUtils.isBlank(userDifficultiesFeedBackRequest.getContent()) || StringUtils.isBlank(userDifficultiesFeedBackRequest.getDifficulties())) {
            return Response.error(BasicErrorCodeEnum.PARAM_VALID_ERROR, "params invalid");
        }
        UserAccessEntity userAccessEntity = userAccessDao.selectUserAccessByUserCode(userDifficultiesFeedBackRequest.getUserCode());
        UserAssistanceFeedbackEntity userAssistanceFeedbackEntity = new UserAssistanceFeedbackEntity();
        userAssistanceFeedbackEntity.setUserCode(userDifficultiesFeedBackRequest.getUserCode());
        userAssistanceFeedbackEntity.setAssistanceContent(userDifficultiesFeedBackRequest.getContent());
        userAssistanceFeedbackEntity.setAppCode(userAccessEntity != null ? userAccessEntity.getAppCode() : "");
        userAssistanceFeedbackEntity.setEmail(userDifficultiesFeedBackRequest.getEmail());
        userAssistanceFeedbackEntity.setName(userDifficultiesFeedBackRequest.getName());
        userAssistanceFeedbackEntity.setStatus(0);
        userAssistanceFeedbackEntity.setMobile(userAccessEntity.getMobile());
        userAssistanceFeedbackEntity.setDifficulties(userDifficultiesFeedBackRequest.getDifficulties());
        userAssistanceFeedbackDao.insert(userAssistanceFeedbackEntity);
        return Response.ok().putData(true);
    }

    @Override
    public Response<UserBehaviorRegister> getRegisterByUserCode(String userCode) {
        UserBehaviorRegisterEntity entity = userBehaviorRegisterDao.selectUserBehaviorRegisterByUserCodeAndAppCode(userCode);
        //LOGGER.info("------ get register by user code, userCode:{}, json:{} ------", userCode, JSONObject.toJSONString(entity));
        if (entity == null) {
            return Response.ok().putData(null);
        }
        UserBehaviorRegister register = new UserBehaviorRegister();
        BeanUtils.copyProperties(entity, register);
        //LOGGER.info("------ get register by user code, copy:{} ------", JSONObject.toJSONString(register));
        return Response.ok().putData(register);
    }

    @Override
    public Response<List<String>> getLatestChannelFromRegister(Integer hour) {
        List<String> strings = userBehaviorRegisterDao.selectLatestRegisterChannelByHour(hour);
        return Response.ok().putData(strings);
    }

    @Override
    public Response<Boolean> addTongDunBodyguardInfo(TongDunBodyguard tongDunBodyguard) {
        if (StringUtils.isBlank(tongDunBodyguard.getUserCode())) {
            throw new ApiErrorException("userCode is empty");
        }
        if (tongDunBodyguard.getBodyguardTypeEnum() == null) {
            throw new ApiErrorException("bodyguardTypeEnum is empty");
        }

        userBehaviorBodyguardDao.deleteByUserCodeAndType(tongDunBodyguard.getUserCode(), tongDunBodyguard.getBodyguardTypeEnum().getType());

        UserBehaviorBodyguardEntity entity = new UserBehaviorBodyguardEntity();
        entity.setUserCode(tongDunBodyguard.getUserCode());
        entity.setType(tongDunBodyguard.getBodyguardTypeEnum().getType());
        entity.setBodyguardId(tongDunBodyguard.getBodyguardId());
        entity.setBodyguardResult(tongDunBodyguard.getBodyguardResult());
        userBehaviorBodyguardDao.insert(entity);

        return Response.ok().putData(true);
    }

    @Override
    public Response<Boolean> getUserWhetherLogin(String userCode) {
        UserBehaviorLoginEntity userBehaviorLoginEntity = userBehaviorLoginDao.queryUserLatestBehaviorLogin(userCode);
        if (userBehaviorLoginEntity == null) {
            return Response.ok().putData(false);
        }

        return Response.ok().putData(true);
    }

    @Override
    public Response<Boolean> insertUserDailyActivityInfo(UserDailyActivityInfo userDailyActivityInfo) {
        if (userDailyActivityInfo == null || StringUtils.isEmpty(userDailyActivityInfo.getUserCode())) {
            return Response.error(BasicErrorCodeEnum.PARAM_VALID_ERROR, "param invalid");
        }
        UserDailyActivityInfoEntity entity = new UserDailyActivityInfoEntity();
        BeanUtils.copyProperties(userDailyActivityInfo, entity);
        entity.setCreateDate(new Date());
        userDailyActivityInfoDao.insert(entity);
        return null;
    }

    @Override
    public Response<Boolean> insertUserBehaviorEvent(UserBehaviorEventRequest userBehaviorEventRequest) {
        if (userBehaviorEventRequest == null ||
                StringUtils.isBlank(userBehaviorEventRequest.getUserCode()) ||
                StringUtils.isBlank(userBehaviorEventRequest.getEvent())) {
            return Response.error(BasicErrorCodeEnum.PARAM_VALID_ERROR, "param is invalid");
        }
        UserBehaviorEventEntity entity = new UserBehaviorEventEntity();
        BeanUtils.copyProperties(userBehaviorEventRequest, entity);
        userBehaviorEventDao.insert(entity);
        return Response.ok().putData(true);
    }

    /**
     * 用户埋点
     *
     * @param userBehaviorEvent
     * @return
     */
    @Override
    public Response<Boolean> addUserEventTrack(UserBehaviorEvent2Request userBehaviorEvent) {
        UserBehaviorBuriedEntity userBehaviorBuriedEntity = new UserBehaviorBuriedEntity();
        userBehaviorBuriedEntity.setDistinctId(userBehaviorEvent.getUserCode());
        userBehaviorBuriedEntity.setEvent(userBehaviorEvent.getUserEventTrack().getEvent());
        userBehaviorBuriedEntity.setProperties(userBehaviorEvent.getProperties() == null ? "" : JSON.toJSONString(userBehaviorEvent.getProperties()));
        userBehaviorBuriedDao.insert(userBehaviorBuriedEntity);
        return Response.ok().putData(true);
    }

    @Override
    public Response<Boolean> addThirdCallHistory(ThirdCallHistoryRequest thirdCallHistoryRequest) {
        if(thirdCallHistoryRequest == null || StringUtils.isBlank(thirdCallHistoryRequest.getServiceCode())
                || StringUtils.isBlank(thirdCallHistoryRequest.getServiceName()) || StringUtils.isBlank(thirdCallHistoryRequest.getInterfaceName())){
            return Response.error(BasicErrorCodeEnum.PARAM_VALID_ERROR, "param is invalid");
        }
        ThirdCallHistoryEntity entity = new ThirdCallHistoryEntity();
        BeanUtils.copyProperties(thirdCallHistoryRequest, entity);
        thirdCallHistoryDao.insert(entity);
        return Response.ok().putData(true);
    }

    @Override
    public Response<Boolean> saveUserOpenBlack(UserOpenBlackListRequest userOpenBlackListRequest) {
        if(userOpenBlackListRequest == null || StringUtils.isBlank(userOpenBlackListRequest.getMd5Mobile())
                || StringUtils.isBlank(userOpenBlackListRequest.getOrgId())){
            return Response.error(BasicErrorCodeEnum.PARAM_VALID_ERROR, "param is invalid");
        }
        OpenApiBlackListMatchingResultEntity entity = new OpenApiBlackListMatchingResultEntity();
        BeanUtils.copyProperties(userOpenBlackListRequest, entity);
        openApiBlackListMatchingResultDao.insert(entity);
        return Response.ok().putData(true);
    }
}
